package public

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
)

func (c *Controller) Organizations() {

	var organizations []models.Organization

	offset := c.GetString("offset")
	limit := c.GetString("limit")
	if limit == "" || limit == "0" {
		db.DB.Find(&organizations)
	} else {
		db.DB.Limit(limit).Offset(offset).Find(&organizations)
	}

	var total int
	db.DB.Model(&models.Organization{}).Count(&total)
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["total"] = total
	result["organizations"] = organizations
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *Controller) OrganizationQuery() {
	name := c.GetString("organization_name")
	if name == "" {
		c.ResponseFailed(201, "请输入需要查询的社团名称")
		return
	}

	var organizations []models.Organization
	db.DB.Where("name like ?", "%"+name+"%").Find(&organizations)
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["organizations"] = organizations
	c.Data["json"] = result
	c.ServeJSON()
}
